import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/components/common/no_permissions_widget.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/assets.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/core/values/gaps.dart';

import 'company_info_controller.dart';

///企业基本信息
class CompanyInfoPage extends StatelessWidget {
  CompanyInfoPage({super.key});

  final controller = Get.put(CompanyInfoController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        if (!controller.isNotPermissions) {
          return const NoPermissionsWidget(boolTOP:false);
        }
        return Scaffold(
          body: Container(
            color: const Color(0xFFF7FBFE),
            padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
            child: ListView(
              children: [
                CustomTitleChartPage(
                  title: controller.data?.enterpriseName ?? '',
                  child: itemWeight(),
                ),
                Container(
                  margin: EdgeInsets.only(top: 16.w),
                  padding: EdgeInsets.all(16.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  child: Column(
                    children: [
                      _title('营业执照证件', ''),
                      AppGaps.vGap8,
                      controller.data?.businessLicense == null || controller.data!.businessLicense!.isEmpty || controller.data!.businessLicense == 'non ansequat'
                          ? Container()
                          : _show(controller.data!.businessLicense!),
                      AppGaps.vGap8,
                      _title('营业期限', '${controller.data?.businessTermStart?.split('T').first ?? ''}-${controller.data?.businessTermEnd?.split('T').first ?? ''}'),
                      AppGaps.vGap8,
                      _title('营业期限到期前', '${controller.data?.businessTermRemind ?? ''}月-${controller.data?.businessRemindId == 1 ? '站内信' : '站内信'} 提醒'), //1短信 2站内信
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 16.w),
                  padding: EdgeInsets.all(16.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  child: Column(
                    children: [
                      _title('安全许可证', ''),
                      AppGaps.vGap8,
                      controller.data?.securotyLicense == null || controller.data!.securotyLicense!.isEmpty || controller.data!.securotyLicense == 'laboris tation do'
                          ? Container()
                          : Column(children: controller.data!.informationDOList.map((e)=>_showFist(e.managementPicture)).toList(),),
                      AppGaps.vGap8,
                      _title('营业期限', '${controller.data?.securotyTermStart?.split('T').first ?? ''}-${controller.data?.securotyTermEnd?.split('T').first ?? ''}'),
                      AppGaps.vGap8,
                      _title('营业期限到期前', '${controller.data?.securotyTermRemind ?? ''}月-${controller.data?.securotyRemindId == 1 ?  '站内信' : '站内信'} 提醒'),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 16.w),
                  padding: EdgeInsets.all(16.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  child: Column(
                    children: [
                      _title('危险化学品经营许可证', ''),
                      AppGaps.vGap8,
                      controller.data?.dangerLicense == null || controller.data!.dangerLicense!.isEmpty || controller.data!.dangerLicense == 'Excepa tempor'
                          ? Container()
                          : _show(controller.data!.dangerLicense!),
                      AppGaps.vGap8,
                      _title('营业期限', '${controller.data?.dangerTermStart?.split('T').first ?? ''}-${controller.data?.dangerTermStart?.split('T').first ?? ''}'),
                      AppGaps.vGap8,
                      _title('营业期限到期前', '${controller.data?.dangerTermRemind ?? ''}月-${controller.data?.dangerRemindId == 1 ? '站内信' : '站内信' } 提醒'),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _title(String title, String? value) {
    return Row(
      children: [
        Text(
          '$title：',
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w400,
            color: const Color(0xFFC7CEE5),
          ),
        ),
        Expanded(
          child: Align(
            alignment: Alignment.centerLeft,
            child: Text(
              value ?? '空',
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xFF1D3452),
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget itemWeight() {
    //对象
    return Column(
      children: [
        _title('单位地址', controller.data?.enterpriseAddress ?? ''),
        AppGaps.vGap8,
        _title('统一社会信用代码', controller.data?.creditCode ?? ''),
        AppGaps.vGap8,
        _title('分管安全负责人', controller.data?.securityManager ?? ''),
        AppGaps.vGap8,
        _title('分管安全负责人手机号', controller.data?.securityManagerNumber ?? ''),
        AppGaps.vGap8,
        _title('企业网站', controller.data?.enterpriseWeb ?? ''),
        AppGaps.vGap8,
        _title('法定代表人', controller.data?.legalRepresentative ?? ''),
        AppGaps.vGap8,
        _title('法定代表人手机号', controller.data?.legalRepresentativeNumber ?? ''),
        AppGaps.vGap8,
        Row(
          children: [
            Expanded(child: _title('注册资本', controller.data?.registeredCapital ?? '')),
            Expanded(child: _title('所属行业', controller.data?.industryName ?? '')),
          ],
        ),
        // AppGaps.vGap8,
        // _title('营业执照', ''),
        // AppGaps.vGap8,
        // controller.data?.businessLicense == null || controller.data!.businessLicense!.isEmpty|| controller.data!.businessLicense=='non ansequat'
        //     ? Container()
        //     : SizedBox(
        //         height: 175.w,
        //         child: Image.network(
        //           controller.data!.businessLicense!.startsWith('http:') ? (controller.data?.businessLicense ?? '') : '${Interface.fileUrl} ${controller.data?.businessLicense}',
        //           fit: BoxFit.fill,
        //         ),
        //       ).clickShowLarge(url: '${ controller.data!.businessLicense}'),
      ],
    );
  }

  Widget _show(String path) {
    Widget widget;
    if (FileUtils.getFileType(path) == 'jpg' ||
        FileUtils.getFileType(path) == 'jpeg' ||
        FileUtils.getFileType(path) == 'png' ||
        FileUtils.getFileType(path) == 'gif' ||
        FileUtils.getFileType(path) == 'jfif' ||
        FileUtils.getFileType(path) == 'webp') {
      widget = Column(
        children: path.split(',').map((e) {
          return e.asImg(clickShowLarge: true);
        }).toList()

      );
    } else {
      final String currUrlName = path.toString().substring(25);
      widget = Container(
        margin: EdgeInsets.only(bottom: 10.w),
        child: Text(
          currUrlName,
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xFF5C6DFF),
          ),
        ).clickOnce(() {
          //打开文件
          FileUtils.openFile(path);
        }),
      );
    }
    return widget;
  }
  Widget _showFist(String path) {
    Widget widget;
    if (FileUtils.getFileType(path) == 'jpg' ||
        FileUtils.getFileType(path) == 'jpeg' ||
        FileUtils.getFileType(path) == 'png' ||
        FileUtils.getFileType(path) == 'gif' ||
        FileUtils.getFileType(path) == 'jfif' ||
        FileUtils.getFileType(path) == 'webp') {
      widget =  path.asImg(clickShowLarge: true);
    } else {
      final String currUrlName = path.toString().substring(25);
      widget = Container(
        margin: EdgeInsets.only(bottom: 10.w),
        child: Text(
          currUrlName,
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xFF5C6DFF),
          ),
        ).clickOnce(() {
          //打开文件
          FileUtils.openFile(path);
        }),
      );
    }
    return widget;
  }
}
